package com.siwei.eqi.service.sys.impl;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.siwei.eqi.mapper.sys.SysRegionMapper;
import com.siwei.eqi.pojo.entity.system.SysRegion;
import com.siwei.eqi.service.sys.SysRegionService;
import com.siwei.eqi.util.HttpUtil;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.*;

@Service
public class SysRegionServiceImpl extends ServiceImpl<SysRegionMapper, SysRegion> implements SysRegionService {

    @Override
    public void addRedion(SysRegion region) {
        baseMapper.insert(region);
    }

    @Override
    public List<SysRegion> getRegionList(String code) {

        List<SysRegion> regions = new ArrayList<>();
        try {
            String url = "http://219.153.49.10:7810/sj_assets/v6/api/ecomn/region_boundary?op=select&key=&reg_type=";
                    Map<String, String> params = new HashMap<>();
                    params.put("parent_id", code);
                    params.put("ak", "mf1ce8ac07b3ce12dc25a41c3ea4c26339");

                    // 请求省级数据
                    String s = HttpUtil.get(url, params, new HashMap<>());
                    Map<String, Object> parse = (Map<String, Object>) JSON.parse(s);
                    Map<String, Object> result = (Map<String, Object>) parse.get("result");
                    Object itemList1 = result.get("item_list");
                    if (itemList1 instanceof Collection) {

                        List<Map<String, String>> itemList = (List<Map<String, String>>) result.get("item_list");
                        for (Map<String, String> stringStringMap : itemList) {
                    // 创建省级区域
                    SysRegion province = new SysRegion();
                    province.setName(stringStringMap.get("name"));
                    province.setCode(Long.valueOf(stringStringMap.get("code")));
//                province.setParentCode(0L);
//                province.setRegType("省份");
                    // 将省级区域加入 provinces 列表
                    regions.add(province);
//                // 获取市级区域
//                params.put("parent_id", stringStringMap.get("code"));
//                s = HttpUtil.get(url, params, new HashMap<>());
//                Map<String, Object> city = (Map<String, Object>) JSON.parse(s);
//                Map<String, Object> citys = (Map<String, Object>) city.get("result");
//
//                if (citys.containsKey("item_list") && citys.get("item_list") instanceof List) {
//                    List<Map<String, String>> cityList = (List<Map<String, String>>) citys.get("item_list");
//
//                    for (Map<String, String> stringMap : cityList) {
//                        // 创建市级区域
//                        SysRegion cityRegion = new SysRegion();
//                        cityRegion.setCode(Long.valueOf(stringMap.get("code")));
//                        cityRegion.setName(stringMap.get("name"));
//                        cityRegion.setProvince(stringStringMap.get("name"));
//                        cityRegion.setParentCode(Long.valueOf(stringStringMap.get("code")));
//                        cityRegion.setRegType("地市");
//
//                        // 将市级区域加入省级区域的 regions
//                        province.getRegions().add(cityRegion);
//
//                        // 获取区县级区域
//                        params.put("parent_id", stringMap.get("code"));
//                        String s1 = HttpUtil.get(url, params, new HashMap<>());
//                        Map<String, Object> county = (Map<String, Object>) JSON.parse(s1);
//                        Map<String, Object> countys = (Map<String, Object>) county.get("result");
//
//                        if (countys.containsKey("item_list") && countys.get("item_list") instanceof List) {
//                            List<Map<String, String>> countyList = (List<Map<String, String>>) countys.get("item_list");
//
//                            for (Map<String, String> map : countyList) {
//                                // 创建区县级区域
//                                SysRegion countyRegion = new SysRegion();
//                                countyRegion.setCode(Long.valueOf(map.get("code")));
//                                countyRegion.setName(map.get("name"));
//                                countyRegion.setProvince(stringStringMap.get("name"));
//                                countyRegion.setParentCode(Long.valueOf(stringMap.get("code")));
//                                countyRegion.setCity(stringMap.get("name"));
//                                countyRegion.setRegType("区县");
//
//                                // 将区县级区域加入市级区域的 regions
//                                cityRegion.getRegions().add(countyRegion);
//                            }
//                        }
//                    }
//                }
                }
            }

        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return regions;
    }
}
